package com.community.itelligentcommunity.sys.controller;

import com.community.itelligentcommunity.common.base.constant.SystemStaticConst;
import com.community.itelligentcommunity.common.base.entity.Page;
import com.community.itelligentcommunity.common.util.json.JsonHelper;
import com.community.itelligentcommunity.sys.entity.QueryTestFile;
import com.community.itelligentcommunity.sys.entity.Testfile;
import com.community.itelligentcommunity.sys.service.TestFileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.lang.reflect.ParameterizedType;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/testFile")
public class TestFileController {
    @Autowired
    private TestFileService testFileService;


    /**
     * 功能描述：直接跳转到更新数据的页面
     *
     * @param entity
     * @return
     */
    @RequestMapping(value = "/updatePage", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public String updatePage(Testfile entity, Model model) throws Exception {
        entity = testFileService.get(entity);
        model.addAttribute("entity", entity);
        return "sys/testFile/update";

    }

    /**
     * 跳转到添加对象页面
     *
     * @throws Exception
     */
    @RequestMapping(value = "/addPage")
    public String addPage() throws Exception {
        return "sys/testFile/add";
    }

    /**
     * 功能描述：保存数据数据
     *
     * @param entity
     * @return
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public Map<String, Object> save(Testfile entity) throws Exception {
        boolean success = testFileService.save(entity);
        Map<String, Object> result = new HashMap<String, Object>();
        if (success == true) {
            result.put(SystemStaticConst.RESULT, SystemStaticConst.SUCCESS);
            result.put(SystemStaticConst.MSG, "增加数据成功！");
            result.put("entity", entity);
        } else {
            result.put(SystemStaticConst.RESULT, SystemStaticConst.FAIL);
            result.put(SystemStaticConst.MSG, "增加数据失败！");
        }
        return result;
    }

    /**
     * 功能描述：更新数据字典数据
     *
     * @param entity
     * @return
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public Map<String, Object> update(Testfile entity) throws Exception {
        boolean success = testFileService.update(entity);
        Map<String, Object> result = new HashMap<String, Object>();
        if (success == true) {
            result.put(SystemStaticConst.RESULT, SystemStaticConst.SUCCESS);
            result.put(SystemStaticConst.MSG, "更新数据成功！");
            result.put("entity", entity);
        } else {
            result.put(SystemStaticConst.RESULT, SystemStaticConst.FAIL);
            result.put(SystemStaticConst.MSG, "更新数据失败！");
        }
        return result;
    }

    /**
     * 功能描述：实现批量删除数据字典的记录
     *
     * @param entity
     * @return
     */
    @RequestMapping(value = "/remove", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public Map<String, Object> remove(Testfile entity) throws Exception {
        Map<String, Object> result = new HashMap<String, Object>();
        testFileService.delete(entity);
        result.put(SystemStaticConst.RESULT, SystemStaticConst.SUCCESS);
        result.put(SystemStaticConst.MSG, "删除数据成功！");
        return result;
    }


    /**
     * 功能描述：实现批量删除数据字典的记录
     *
     * @param json
     * @return
     */
    @RequestMapping(value = "/removeBath", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public Map<String, Object> removeBath(String json) throws Exception {
        Map<String, Object> result = new HashMap<String, Object>();
        //将传入的字符串解析成一个集合，实现批量删除
        if (testFileService.removeBath((List<Testfile>) JsonHelper.toList(json, (Class<Testfile>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0]))) {
            result.put(SystemStaticConst.RESULT, SystemStaticConst.SUCCESS);
            result.put(SystemStaticConst.MSG, "删除数据成功！");
        } else {
            result.put(SystemStaticConst.RESULT, SystemStaticConst.FAIL);
            result.put(SystemStaticConst.MSG, "删除数据失败！");
        }
        return result;
    }

    /**
     * 功能描述：获取数据字典的分页的数据
     *
     * @param entity
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public Map<String, Object> list(QueryTestFile entity) {
        Map<String, Object> result = new HashMap<String, Object>();
        Page page = testFileService.findByPage(entity);
        result.put("totalCount", page.getTotal());
        result.put("result", page.getRows());
        return result;
    }

    /**
     * 功能描述：判断当前的字典元素是否已经存在
     *
     * @param entity
     * @return
     */
    @RequestMapping(value = "/isExist", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public Map<String, Object> isExist(QueryTestFile entity) {
        Map<String, Object> result = new HashMap<String, Object>();
        if (testFileService.query(entity).size() > 0) {
            result.put("valid", false);
        } else {
            result.put("valid", true);
        }
        return result;
    }

    /**
     * 功能描述：查询
     *
     * @param queryModel
     * @return
     */
    @RequestMapping(value = "/query")
    @ResponseBody
    public List<Testfile> query(QueryTestFile queryModel) {
        return testFileService.query(queryModel);
    }

    /**
     * 功能描述：获取实体类
     *
     * @param entity
     * @return
     */
    @RequestMapping(value = "/get")
    @ResponseBody
    public Testfile get(Testfile entity) {

        return testFileService.get(entity);
    }

    /**
     * 将首字母变小写
     *
     * @param str
     * @return
     */
    private String toFirstCharLowerCase(String str) {
        char[] columnCharArr = str.toCharArray();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < columnCharArr.length; i++) {
            char cur = columnCharArr[i];
            if (i == 0) {
                sb.append(Character.toLowerCase(cur));
            } else {
                sb.append(cur);
            }
        }
        return sb.toString();
    }


}
